package com.duleme.servlet;

import com.duleme.common.Config;
import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.entity.Shop;
import com.google.gson.Gson;
import com.duleme.entity.Book;
import com.duleme.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@MultipartConfig
@WebServlet(urlPatterns = "/upload/book")
public class UploadNewBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EncodeUtils.setEncoding(req,resp);
        Book book=new Book();
        Shop shop=(Shop)req.getSession().getAttribute("shop");
        String bookName=req.getParameter("bookName");
        String author=req.getParameter("author");
        String press=req.getParameter("press");
        int remain=Integer.parseInt(req.getParameter("bookNum"));
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date publishDate=null;
        try {
            publishDate = format1.parse(req.getParameter("publishDate"));
        }catch (Exception e){
            e.getStackTrace();
        }
        String category= URLDecoder.decode(req.getParameter("category"),"utf-8");
        System.out.println(category);
        String synopsis=req.getParameter("describe");
        int categoryId= Config.CATEGORY.get(category);//需要改为获取类别对应的id
        String shopName=shop.getShopName();
        int shopId=shop.getShopId();
        String savePath =Config.COVER_SAVE_PATH;
        //获取上传的文件集合
        Part part=req.getPart("upload");
        //savePath+ File.separator+shopName+"_"+bookName;
        String accessPath=Config.COVER_ACCESS_PATH;
        String fileName=shopName+"_"+bookName+".jpg";
        Double price=Double.parseDouble(req.getParameter("price"));
        if (part!=null) {
            part.write(savePath + File.separator + fileName);
            book.setCover(accessPath+fileName);
        }else book.setCover(Config.COVER_DEFAULT_ACCESS_PATH);
        book.setBookName(bookName);
        book.setShopName(shopName);
        book.setPress(press);
        book.setAuthor(author);
        book.setSynopsis(synopsis);
        book.setShopId(shopId);
        book.setCategory(category);
        book.setCategoryId(categoryId);
        book.setPublishDate(publishDate);
        book.setRemain(remain);
        book.setPrice(price);
        book.setSell(0);
        BookServiceImpl bookService=new BookServiceImpl();
        boolean result=bookService.uploadNewBook(book);
        //resp.getWriter().print(JsonUtils.result2Json(result?"1":"0"));
        resp.sendRedirect("http://localhost:8080/shopUpperShelf.html");
    }
}
